#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "person.h"

#include <QtGui/QMainWindow>
#include <QDate>
#include <QSqlDatabase>
#include <QMap>
#include <QDir>

#include "recordedtime.h"
#include "ticksheet.h"
#include "participantsheet.h"

class QTime;
class QTimer;
class QTreeView;
class QStandardItemModel;
class QSqlTableModel;
class QCompleter;

namespace Ui
{
    class MainWindowClass;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:

    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:

    //Timer slots
    void on_startButton_clicked();
    void on_restartButton_clicked();
    void on_recordButton_clicked();
    void on_noteEdit_returnPressed();
    void on_noteEdit_textChanged ( const QString & text );
    void on_actionSave_triggered();
    void on_actionQuickSave_triggered();
    void on_actionToggleWarning_triggered();
    void on_actionRecord_triggered();
    void on_timerChanged(const QString&);

    // Participants
    void on_nameEdit_returnPressed();
    void on_editParticipantButton_clicked() ;
    void on_addParticipantButton_clicked();
    void on_insertParticipantButton_clicked();
    void on_removeParticipantButton_clicked();
    void on_moveUpButton_clicked();
    void on_moveDownButton_clicked();
    void slot_participantlist_changed();

    //Setup/config slots
    void on_importButton_clicked();

    //Results panel
    void slot_buildResultslist();

    // Makes a general directory to write files
    void makeDir();

protected:

    QString calcTimeString(int);
    void    saveFile(QString filename);
    void    initializeAutoCompleteModel();
    void    importDatabase();
    bool    createConnection();
    void    enableNameAutoComplete(bool enabled);
    void    clearParticipantForm();
    void    editParticipant(Participant*);
    int     getNextBibNumber(int);
    Participant* makeParticipantFromForm();
    void    editParticipantInForm(Participant*, int row, bool edit);

private:

    Ui::MainWindowClass *ui;

    // Auto-complete
    QCompleter*            mNameCompleter;
    QCompleter*            mCityCompleter;
    QMap<QString, Person*> mNameCompleteDatabase;

    // Participants
    ParticipantSheet*    mCurrentParticipantSheet;
    QStandardItemModel*  mParticipantsModel;
    QStringList          mParticaipantHeaderLabels;

    // Timer
    Ticksheet*           mCurrentTicksheet;
    QTreeView*           mTreeView;
    QStandardItemModel*  mRecordedTimesModel;

    // Results
    QStandardItemModel*  mResultsModel;
    QStringList          mResultsHeaderLabels;


    bool   mIsRunning;
    int    mParticipantFormPosition;

    QDir   mRootDir;
};

#endif // MAINWINDOW_H
